package com.zzj.ecology.system.service.impl.system;

import com.zzj.ecology.common.constant.GlobalConstant;
import com.zzj.ecology.system.bean.system.SysPermission;
import com.zzj.ecology.system.bean.system.SysPermissionExample;
import com.zzj.ecology.system.dto.HttpResult;
import com.zzj.ecology.system.domain.Page;
import com.zzj.ecology.system.bean.vo.dtree.DTree;
import com.zzj.ecology.system.bean.vo.dtree.DTreeResponse;
import com.zzj.ecology.system.mapper.system.SysPermissionMapper;
import com.zzj.ecology.system.service.system.SysPermissionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName SysPermissionServiceImpl
 * @Description TODO
 * @Author zhijun zeng at 2020/2/28
 * @Version 1.0
 **/
@Service("sysPermissionService")
@Transactional(rollbackFor = Exception.class)
public class SysPermissionServiceImpl implements SysPermissionService {

    public static final Logger logger = LoggerFactory.getLogger(SysPermissionServiceImpl.class);

    @Autowired
    private SysPermissionMapper sysPermissionMapper;


    /**
     * 查询 所有 顶级权限 及 对应的 子级权限
     * @return
     */
    public List<SysPermission> findPermissionListSelectDisplay() {
        List<SysPermission> list = sysPermissionMapper.findParentsListDetail();
        return list;
    }


    /**
     * 分页查询 权限 列表数据
     * @param page
     * @return
     */
    public HttpResult findPageList(Page page) {
        int currentPage = page.getCurrentPage();
        Integer pageCounts = page.getPageCounts();
        int first = (currentPage - 1) * pageCounts;

        List<SysPermission> lists = sysPermissionMapper.findPageList(first, pageCounts);

        page.getData().put(HttpResult.LIST_TAG, lists);// 当前页面数据
        SysPermissionExample example = new SysPermissionExample();
        example.createCriteria().andDeleteStatusEqualTo(GlobalConstant.DELETE_STATUS_NOT_DELETE);
        List<SysPermission> list1 = sysPermissionMapper.selectByExample(example);//不分页总数据
        page.setTotalCounts(list1.size());

        return HttpResult.success().add(HttpResult.PAGE_TAG, page);
        //return HttpResult.error().add(Constant.RESP_ERRCODE_KEY, "查询不到数据");
    }

    /**
     * 查询 Dtree 数据结构,用于前端 下拉框选择, 查询 delete_status = '0' 表示未删除的数据
     * @return
     */
    public DTreeResponse dtree() {
        List<DTree> dtrees = sysPermissionMapper.dtree();
        DTreeResponse response = new DTreeResponse();
        response.setData(dtrees);
        return response;
    }

    /**
     * 创建新的权限记录
     * @param sysPermission
     * @return
     */
    public HttpResult create(SysPermission sysPermission) {
        int i = sysPermissionMapper.insertSelective(sysPermission);
        return HttpResult.success();
    }

    /**
     * 修改权限
     * @param sysPermission
     * @return
     */
    public HttpResult update(SysPermission sysPermission) {
        int i = sysPermissionMapper.updateByPrimaryKeySelective(sysPermission);
        return HttpResult.success();
    }

    /**
     * 批量删除权限， 标记删除
     * @param userids
     * @param username
     * @return
     */
    public HttpResult updateDeleteStatusByIds(List<Integer> userids, String username) {
        int i = sysPermissionMapper.updateDeleteStatusByIds(userids, username);
        return HttpResult.success();
    }

    /**
     * 根据 urlid 查询关联的 permission
     * @param urlId
     * @return
     */
    public SysPermission findByUrlId(Integer urlId) {
        List<SysPermission> perms = sysPermissionMapper.findUrlPermissionRelationByUrlId(urlId);
        return perms.size() > 0 ? perms.get(0) : null;
    }


//
//    /**
//     * 根据 用户ID 查询 此用户的权限
//     * @param id
//     * @return
//     */
//    public List<SysPermission> findListByUserId(Integer id) {
//        List<SysPermission> list = sysPermissionMapper.findListByUserId(id);
//        return list;
//    }
//

//
//    /**
//     * 查询 所有权限列表，非分页数据
//     * @return
//     */
//    public HttpResult findParentsListDetail() {
//        List<SysPermission> permissions = sysPermissionMapper.findParentsListDetail();
//        return permissions.size() > 0 ? HttpResult.success().add(Constant.RESP_DATA_KEY, permissions) : HttpResult.error();
//    }
//
//    /**
//     * 根据 ROLEID 角色 ID 查询 Permission 权限列表
//     * @param roleId
//     * @return
//     */
//    public HttpResult findListByRoleId(Integer roleId) {
//        List<SysPermission> list = sysPermissionMapper.findListByRoleId(roleId);
//        return list.size() > 0 ? HttpResult.success().add(Constant.RESP_DATA_KEY, list) : HttpResult.error();
//    }
//

//
//    /**
//     * 查询 所有 非 TOP 权限, 不映射子集，父类，映射普通字段。即 pid != 0
//     * 不映射子集，父类，映射普通字段
//     * @return
//     */
//    public HttpResult findWithoutParentsList() {
//        List<SysPermission> list = sysPermissionMapper.findWithoutParentsList();
//        return list.size() > 0 ? HttpResult.success().add(Constant.RESP_DATA_KEY, list) : HttpResult.error();
//    }
//
//
//
//
//    /**
//     * 根据 permissionCode 查询 数据库 是否存在相同值 ，如果存在则返回false通知前端不可使用
//     * @param permissionCode
//     * @return
//     */
//    public boolean isNotExistRoleCode(String permissionCode) {
//        SysPermissionExample example = new SysPermissionExample();
//        example.createCriteria().andPermissionCodeEqualTo(permissionCode);
//        List<SysPermission> list = sysPermissionMapper.selectByExample(example);
//        return list.size() > 0 ? false : true;
//    }
//
//    /**
//     * 创建 新 权限
//     * @param sysPermission
//     * @return
//     */
//    public HttpResult create(SysPermission sysPermission) {
//        int i = sysPermissionMapper.insertSelective(sysPermission);
//        return i > 0 ? HttpResult.success() : HttpResult.error();
//    }
//
//    /**
//     * 修改 SysPermission
//     * @param sysPermission
//     * @return
//     */
//    public HttpResult update(SysPermission sysPermission) {
//        int i = sysPermissionMapper.updateByPrimaryKeySelective(sysPermission);
//        return i > 0 ? HttpResult.success() : HttpResult.error();
//    }
//
//    /**
//     * 查询 关联表中是否存在数值，存在则一并删除
//     * @param ids
//     * @return
//     */
//    public HttpResult deleteAndUnGrant(Integer[] ids) {
//        for (Integer id : ids) {
//            sysPermissionMapper.deleteByPrimaryKey(id);// delete SysRole Schema
//            sysPermissionMapper.deletePermissionUrlRelationByPermissionId(id);// deleted Relation
//        }
//        return HttpResult.success();
//    }


}
